<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/taglib.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>计划任务参数设置</title>
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <script type="text/javascript">
        var path = "${path}";
        var params = new Object();
        var url = path + "/system/cronjob/queryByList";

        $(function () {
            //设置AjaxLoading
            setAjaxLoading();
            var grid = $("#tt");
            initDataGrid(grid);
        });

        //初始化表格和数据
        function initDataGrid(grid) {
            var columns = [
                {field: "paramsId", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
                {
                    field: "paramName",
                    title: "参数名称",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 150,
                    editor: {
                        type: 'validatebox',
                        options: {
                            required: true,
                            validType: 'maxLength[100]'
                        }
                    }
                },
                {
                    field: "paramCode",
                    title: "参数代码",
                    align: "right",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 150,
                    editor: {
                        type: 'validatebox',
                        options: {
                            required: true,
                            validType: 'maxLength[100]'
                        }
                    }
                },
                {
                    field: "paramValue",
                    title: "参数值",
                    align: "right",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 150,
                    editor: {
                        type: 'validatebox',
                        options: {
                            required: true,
                            validType: 'maxLength[100]',
                            tipPosition: 'left'
                        }
                    }
                },
                {field: "manFlag",
                title: "是否手工 1是/0否",
                align: "right",
                halign: "center",
                sortable: false,
                resizable: true,
                width: 150,
                editor: {
                type: 'validatebox',
                    options: {
                    required: true,
                        validType: 'maxLength[100]',
                        tipPosition: 'left'
                }
            }
        }];
            initGrid(grid, columns, "#tb");
            var gridOptions = $("#tt").datagrid("options");
            gridOptions.onLoadSuccess = function (data) {
                if (data.rows.length > 0) {
                    grid.datagrid('selectRow', 0);
                    grid.datagrid("fixRownumber");
                }
            }
            btnQuery();
        }

        //查询
        function btnQuery() {
            //获取父页面选中的行数据
            var fatherRow = parent.$('#tt').datagrid('getSelections');
            var rowData = fatherRow[0];
            var grid = $("#tt");
            params.jobId = rowData.jobId;
            getData(grid, params, url);
        }

        //新增行
        function btnAdd(params) {
            if (!validatebox()) {
                return;
            }
            var index = $("#tt").datagrid("getRows").length;
            $("#tt").datagrid('insertRow', {
                index: index, row: {status: 'P'},
                editor: {
                    type: 'text'
                }
            });
            $("#tt").datagrid('selectRow', index);
            btnEdit();
        }

        //修改
        var editIndex = undefined;
        function btnEdit() {
            var rows = $('#tt').datagrid('getSelections');
            if (rows.length == 0) {
                $.messager.alert("操作提示", "请选择一条参数！", "info");
                return;
            }
            var index = $('#tt').datagrid('getRowIndex', rows[0]);
            if (editIndex != undefined) {
                if (!validatebox()) {
                    return;
                }
                $('#tt').datagrid('endEdit', editIndex);
                editIndex = undefined;
            }
            $('#tt').datagrid('beginEdit', index);
            editIndex = index;
        }

        //保存
        function btnSave() {
            if (editIndex != undefined) {
                if (!validatebox()) {
                    return;
                }
                $('#tt').datagrid('endEdit', editIndex);
                editIndex = undefined;
            }
            var updateRows = $('#tt').datagrid('getChanges', 'updated');
            var insertRows = $('#tt').datagrid('getChanges', 'inserted');
            //获取父页面选中的行数据
            var fatherRow = parent.$('#tt').datagrid('getSelections');
            var rowData = fatherRow[0];
            if (insertRows.length > 0) {
                for (var i = 0; i < insertRows.length; i++) {
                    var row = insertRows[i];
                    var param = new Object();
                    param.paramsId = row.paramsId;
                    param.jobId = rowData.jobId;
                    param.paramName = row.paramName;
                    param.paramCode = row.paramCode;
                    param.paramValue = row.paramValue;
                    param.manFlag = row.manFlag;
                    btnSaveSubmit(param);
                }
            }
            if (updateRows.length > 0) {
                for (var j = 0; j < updateRows.length; j++) {
                    var row = updateRows[j];
                    var param = new Object();
                    param.paramsId = row.paramsId;
                    param.jobId = rowData.jobId;
                    param.paramName = row.paramName;
                    param.paramCode = row.paramCode;
                    param.paramValue = row.paramValue;
                    param.manFlag = row.manFlag;
                    btnSaveSubmit(param);
                }
            }
            $('#tt').datagrid('acceptChanges');
            $.messager.alert("操作提示", "保存参数成功！", "info", function () {
                btnQuery();
            });
        }

        //validatebox校验
        function validatebox() {
            var f = true;
            $('.validatebox-text').each(function () {
                if (!$(this).validatebox('isValid')) {
                    $(this).focus();
                    f = false;
                }
            });
            return f;
        }

        function btnSaveSubmit(param) {
            $.ajax({
                type: "POST",
                url: path + "/system/cronjob/save",
                data: param,
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data.code != '0000') {
                        alert('获取数据时发生错误：' + data.msg);
                    } else {

                    }
                },
                error: function () {
                    alert('获取数据时发生错误！');
                }
            });
        }

        //删除
        function btnDel() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一条参数~");
                return;
            }
            var param = new Object();
            param.paramsId = row.paramsId;
            $.messager.alert("操作提示", "是否删除？","confirm", function () {

                    $.ajax({
                        type: "POST",
                        url: path + "/system/cronjob/deleteParam",
                        dataType: "json",
                        data: param,
                        async: false,

                        success: function callback(data) {
                            var analyMsg = data;
                            if (analyMsg.code != '0000') {
                                $.messager.alert('操作提示', analyMsg.msg, 'error', function () {
                                });
                            } else {
                                $.messager.alert("操作提示", "删除参数成功！", "info", function () {
                                    btnQuery();
                                    setTimeout("", 100);//设置延时，防止IE下出现异常
                                });
                            }
                        },
                        error: function (data, textstatus) {
                            $.messager.alert('操作提示', data.responseText, 'info', function () {
                            });
                        }
                    });

            });
        }
        //确定
        function btnSubmit() {
            closeLayer();
        }
    </script>
</head>

<body style="margin:0px; padding:0px;">
<div class="easyui-layout" fit="true">
    <div region="center" style="padding: 5px;" border="false">
        <div class="easyui-layout" fit="true" border="false">
            <div region="center" title="" border="false">
                <table id="tt" title="" border="false"></table>
            </div>
        </div>
    </div>
</div>

<div id="tb" style="padding: 1px; height: auto; display: none;">
    <div>
        <a id="btn_addNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false"
           id="btnAdd" onclick="btnAdd()">增加</a>
        <a id="btn_updateNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="false"
           onclick="btnEdit()">修改</a>
        <a id="btn_save_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-save" plain="false"
           onclick="btnSave()">保存</a>
        <a id="btn_removeNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove"
           plain="false" onclick="btnDel()">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="btnSubmit()">确定</a>
    </div>
</div>
</body>
</html>
